* Search inpatient records and keep only the records which apply to AMI patients for 2010 onwards

clear all
set more off

* 2004 - 2008

foreach year in 2004 2005 2006 2007 2008 {
	
use "$rawdata/ip_`year'", clear

cap gen extract_hesid = encrypted_hesid

merge m:1 extract using "$savedata/amipats_ids0517.dta", keepusing(extract_hesid)
keep if _merge==3
drop _merge

* obtain dates from final episode of each spell
foreach var in disdate_day disdate_month disdate_year dismeth {
gen `var'_temp = `var' if spelend=="Y"
egen `var'_f = max(`var'_temp), by(extract admidate_string)
}

replace disdate_year_f=. if disdate_year_f==1600

* length of stay for spell
gen disdate_f = mdy(disdate_month_f, disdate_day_f, disdate_year_f)
gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)
gen spell_los = disdate_f - admidate_f
replace spell_los=. if spell_los<0


gen trust_code = substr(procode,1,3)

gen elective = 0
gen emergency = 1

cap drop diag_01
gen diag_01 = substr(fulldiag3_1,1,4)


*Merge with imputed cost based on primary operation
merge m:1 opertn_01 elective using "$inputs/oper_imp_cost.dta"

gen imputed_cost_op = fce_cost if _m==3
drop if _merge==2
drop _merge 
cap drop fce_cost

*Merge with imputed cost based on primary diagnosis (for those with no operation)

merge m:1 diag_01 elective using "$inputs/diag_no_oper_imp_cost.dta"
* use imputed operation cost if available
replace fce_cost = imputed_cost_op if !missing(imputed_cost_op)
drop if _m==2
drop _merge

* drop the (very few) observations without an imputed cost
keep if fce_cost !=.

replace elective = 1 if admimeth==11 | admimeth==12 | admimeth==13 | admimeth==81

gen fce_cost_em = fce_cost*emergency
gen fce_cost_el = fce_cost*elective 


gen ami`year' = 0
replace ami`year'  = 1 if substr(fulldiag3_1,1,3)=="I21" | substr(fulldiag3_1,1,3)=="I22"

gen stroke`year'  = 0
replace stroke`year'  = 1 if substr(fulldiag2_1,1,2)=="I6"

preserve

collapse (max) ami`year' stroke`year' (sum) fce_cost fce_cost_el fce_cost_em epidur, by(extract)

rename *cost* *cost*`year'
rename epidur epidur`year'

save "$savedata/costs`year'_evercardio.dta", replace


restore

preserve

keep if epiorder==1
gen n_admit`year' = 1
gen n_em_admit`year' = 1 if admimeth<20
gen n_el_admit`year' = 1 if admimeth>19 & admimeth<30

collapse (sum) n_admit n_em_admit n_el_admit, by(extract)

replace n_admit = 365 if n_admit>365
replace n_em_admit = 365 if n_em_admit>365
replace n_el_admit = 365 if n_el_admit>365

save "$savedata/visits`year'_evercardio.dta", replace


restore


preserve
collapse (max) spell_los , by(extract admidate_f)

collapse (sum) spell_los , by(extract)


replace spell_los = 365 if spell_los>365

rename spell_los los`year' 
 
save "$savedata/los`year'_evercardio.dta", replace



restore

* Count diagnoses

preserve


keep if admimeth>19 & admimeth<30

foreach x in 1 2 3 4 5 6 7 8 9 {
gen diag0`x' = substr(fulldiag3_`x',1,4)
}

foreach x in 10 11 12 13 14{
gen diag`x' = substr(fulldiag3_`x',1,4)
}

keep extract diag01 diag02 diag03 diag04 diag05 diag06 diag07 diag08 diag09 diag10 diag11 diag12 diag13 diag14  admidate_f
bys extract admidate_f: gen order=_n

foreach x in 1 2 3 4 5 6 7 8 9{
rename diag0`x' diag`x'
}

reshape long diag, i(extract admidate_f order) j(d)

* Unique combinations of person and primary diagnosis
drop if diag==""
bys extract diag: gen xx=_n
gen z = 0
replace z = 1 if xx==1

egen n_all_emdiag`year'=sum(z), by(extract)

collapse (max) n_all_emdiag`year', by(extract)

save "$savedata/numemdiag`year'_evercardio.dta", replace

restore

use "$savedata/costs`year'_evercardio.dta", clear
cap drop _merge

merge 1:1 extract using "$savedata/visits`year'_evercardio.dta"
rename _merge _mergevisits

replace n_admit=1 if n_admit==.
replace n_em = 1 if n_em==.
replace n_el = 1 if n_el==.

merge 1:1 extract using "$savedata/los`year'_evercardio.dta"
rename _merge _mergelos

merge 1:1 extract using "$savedata/numemdiag`year'_evercardio.dta"
rename _merge _mergediag
replace n_all_emdiag`year' = 0 if n_all_emdiag`year'==.

drop _merge*

save "$savedata/alltreat`year'.dta", replace
}

foreach year in 2009 2010 2011 2012 2013 2014 2015 {

	
use "$rawdata/ip_`year'", clear

cap gen extract_hesid = encrypted_hesid

merge m:1 extract using "$savedata/amipats_ids0517.dta", keepusing(extract_hesid)
keep if _merge==3
drop _merge

cap gen admidate_string = mdy(admidate_month, admidate_day, admidate_year)

* obtain dates from final episode of each spell (GS: also need to obtain final discharge method from final episode)
foreach var in disdate_day disdate_month disdate_year dismeth {
gen `var'_temp = `var' if spelend=="Y"
egen `var'_f = max(`var'_temp), by(extract admidate_string)
}

replace disdate_year_f=. if disdate_year_f==1600

* length of stay for spell
gen disdate_f = mdy(disdate_month_f, disdate_day_f, disdate_year_f)
gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)
gen spell_los = disdate_f - admidate_f
replace spell_los=. if spell_los<0


gen trust_code = substr(procode,1,3)

gen elective = 0
gen emergency = 1

cap drop diag_01
gen diag_01 = substr(fulldiag3_1,1,4)

*Merge with imputed cost based on primary operation
merge m:1 opertn_01 elective using "$inputs/oper_imp_cost.dta"

gen imputed_cost_op = fce_cost if _m==3
drop if _merge==2
drop _merge 
cap drop fce_cost

*Merge with imputed cost based on primary diagnosis (for those with no operation)
merge m:1 diag_01 elective using "$inputs/diag_no_oper_imp_cost.dta"
* use imputed operation cost if available
replace fce_cost = imputed_cost_op if !missing(imputed_cost_op)
drop if _m==2
drop _merge

* drop the (very few) observations without an imputed cost
keep if fce_cost !=.

cap replace admimeth="29" if admimeth=="2A" | admimeth=="2B" | admimeth=="2C" | admimeth=="2D"
cap destring admimeth, replace

replace elective = 1 if admimeth==11 | admimeth==12 | admimeth==13 | admimeth==81

gen fce_cost_em = fce_cost*emergency
gen fce_cost_el = fce_cost*elective 


gen ami`year' = 0
replace ami`year'  = 1 if substr(fulldiag3_1,1,3)=="I21" | substr(fulldiag3_1,1,3)=="I22"

gen stroke`year'  = 0
replace stroke`year'  = 1 if substr(fulldiag2_1,1,2)=="I6"

preserve

collapse (max) ami`year' stroke`year' (sum) fce_cost fce_cost_el fce_cost_em epidur, by(extract)

rename *cost* *cost*`year'
rename epidur epidur`year'

save "$savedata/costs`year'_evercardio.dta", replace

restore

preserve
keep if epiorder==1
gen n_admit`year' = 1
gen n_em_admit`year' = 1 if admimeth<20
gen n_el_admit`year' = 1 if admimeth>19 & admimeth<30

collapse (sum) n_admit n_em_admit n_el_admit, by(extract)

replace n_admit = 365 if n_admit>365
replace n_em_admit = 365 if n_em_admit>365
replace n_el_admit = 365 if n_el_admit>365
save "$savedata/visits`year'_evercardio.dta", replace

restore


preserve
collapse (max) spell_los , by(extract admidate_f)

collapse (sum) spell_los , by(extract)

replace spell_los = 365 if spell_los>365

rename spell_los los`year' 
 
save "$savedata/los`year'_evercardio.dta", replace

restore

* Count diagnoses

preserve


keep if admimeth>19 & admimeth<30

foreach x in 1 2 3 4 5 6 7 8 9 {
gen diag0`x' = substr(fulldiag3_`x',1,4)
}

foreach x in 10 11 12 13 14{
gen diag`x' = substr(fulldiag3_`x',1,4)
}

*keep if epiorder==1
keep extract diag01 diag02 diag03 diag04 diag05 diag06 diag07 diag08 diag09 diag10 diag11 diag12 diag13 diag14  admidate_f
bys extract admidate_f: gen order=_n

foreach x in 1 2 3 4 5 6 7 8 9{
rename diag0`x' diag`x'
}

reshape long diag, i(extract admidate_f order) j(d)

* Unique combinations of person and primary diagnosis
drop if diag==""
bys extract diag: gen xx=_n
gen z = 0
replace z = 1 if xx==1

egen n_all_emdiag`year'=sum(z), by(extract)

collapse (max) n_all_emdiag`year', by(extract)

save "$savedata/numemdiag`year'_evercardio.dta", replace

restore

use "$savedata/costs`year'_evercardio.dta", clear
cap drop _merge

merge 1:1 extract using "$savedata/visits`year'_evercardio.dta"
rename _merge _mergevisits

replace n_admit=1 if n_admit==.
replace n_em = 1 if n_em==.
replace n_el = 1 if n_el==.

merge 1:1 extract using "$savedata/los`year'_evercardio.dta"
rename _merge _mergelos

merge 1:1 extract using "$savedata/numemdiag`year'_evercardio.dta"
rename _merge _mergediag
replace n_all_emdiag`year' = 0 if n_all_emdiag`year'==.

drop _merge*

save "$savedata/alltreat`year'.dta", replace


}


* 16-17

set more off
foreach year in 2016 2017 {
	
use "$rawdata/ip_`year'", clear

cap gen extract_hesid = encrypted_hesid

merge m:1 extract using "$savedata/amipats_ids0517.dta", keepusing(extract_hesid)
keep if _merge==3
drop _merge


* exclusions: remove duplicated episodes (should not have multiple episodes with same epiorder)
foreach date in admidate disdate{
gen `date'_year = substr(`date',1,4)
gen `date'_month = substr(`date',6,2)
gen `date'_day = substr(`date',9,2)

destring `date'_year `date'_month `date'_day, replace
}

gen admidate_string = mdy(admidate_month, admidate_day, admidate_year)

* obtain dates from final episode of each spell (GS: also need to obtain final discharge method from final episode)
foreach var in disdate_day disdate_month disdate_year dismeth {
gen `var'_temp = `var' if spelend=="Y"
egen `var'_f = max(`var'_temp), by(extract admidate_string)
}

replace disdate_year_f=. if disdate_year_f==1600

* length of stay for spell
gen disdate_f = mdy(disdate_month_f, disdate_day_f, disdate_year_f)
gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)
gen spell_los = disdate_f - admidate_f
replace spell_los=. if spell_los<0


gen trust_code = substr(procode,1,3)

gen elective = 0
gen emergency = 1

gen temp_diag01 = substr(diag_01,1,4)
replace diag_01 = temp_diag01

*Merge with imputed cost based on primary operation
merge m:1 opertn_01 elective using "$inputs/oper_imp_cost.dta"

gen imputed_cost_op = fce_cost if _m==3
drop if _merge==2
drop _merge 
cap drop fce_cost

*Merge with imputed cost based on primary diagnosis (for those with no operation)
merge m:1 diag_01 elective using "$inputs/diag_no_oper_imp_cost.dta"
* use imputed operation cost if available
replace fce_cost = imputed_cost_op if !missing(imputed_cost_op)
drop if _m==2
drop _merge

* drop the (very few) observations without an imputed cost
keep if fce_cost !=.

cap replace admimeth="29" if admimeth=="2A" | admimeth=="2B" | admimeth=="2C" | admimeth=="2D"
cap destring admimeth, replace

replace elective = 1 if admimeth==11 | admimeth==12 | admimeth==13 | admimeth==81

gen fce_cost_em = fce_cost*emergency
gen fce_cost_el = fce_cost*elective 


gen ami`year' = 0
replace ami`year'  = 1 if substr(diag_01,1,3)=="I21" | substr(diag_01,1,3)=="I22"

gen stroke`year'  = 0
replace stroke`year'  = 1 if substr(diag_01,1,2)=="I6"

preserve

collapse (max) ami`year' stroke`year' (sum) fce_cost fce_cost_el fce_cost_em epidur, by(extract)

rename *cost* *cost*`year'
rename epidur epidur`year'

save "$savedata/costs`year'_evercardio.dta", replace

restore

preserve
keep if epiorder==1
gen n_admit`year' = 1
gen n_em_admit`year' = 1 if admimeth<20
gen n_el_admit`year' = 1 if admimeth>19 & admimeth<30

collapse (sum) n_admit n_em_admit n_el_admit, by(extract)

replace n_admit = 365 if n_admit>365
replace n_em_admit = 365 if n_em_admit>365
replace n_el_admit = 365 if n_el_admit>365
save "$savedata/visits`year'_evercardio.dta", replace

restore


preserve
collapse (max) spell_los , by(extract admidate_f)

collapse (sum) spell_los , by(extract)

replace spell_los = 365 if spell_los>365

rename spell_los los`year' 
 
save "$savedata/los`year'_evercardio.dta", replace

restore

* Count diagnoses

preserve


keep if admimeth>19 & admimeth<30

foreach x in 01 02 03 04 05 06 07 08 09 10 11 12 13 14{
gen diag`x' = substr(diag_`x',1,4)
}

*keep if epiorder==1
keep extract diag01 diag02 diag03 diag04 diag05 diag06 diag07 diag08 diag09 diag10 diag11 diag12 diag13 diag14  admidate_f
bys extract admidate_f: gen order=_n

foreach x in 1 2 3 4 5 6 7 8 9{
rename diag0`x' diag`x'
}

reshape long diag, i(extract admidate_f order) j(d)

* Unique combinations of person and primary diagnosis
drop if diag==""
bys extract diag: gen xx=_n
gen z = 0
replace z = 1 if xx==1

egen n_all_emdiag`year'=sum(z), by(extract)

collapse (max) n_all_emdiag`year', by(extract)

save "$savedata/numemdiag`year'_evercardio.dta", replace

restore

use "$savedata/costs`year'_evercardio.dta", clear
cap drop _merge

merge 1:1 extract using "$savedata/visits`year'_evercardio.dta"
rename _merge _mergevisits

replace n_admit=1 if n_admit==.
replace n_em = 1 if n_em==.
replace n_el = 1 if n_el==.

merge 1:1 extract using "/$savedata/los`year'_evercardio.dta"
rename _merge _mergelos

merge 1:1 extract using "$savedata/numemdiag`year'_evercardio.dta"
rename _merge _mergediag
replace n_all_emdiag`year' = 0 if n_all_emdiag`year'==.

drop _merge*

save "$savedata/alltreat`year'.dta", replace


}



use "$savedata/alltreat2004.dta", clear

forval year=2005(1)2017{
merge 1:1 extract using "$savedata/alltreat`year'.dta"
drop _merge
}

keep extract ami* stroke* fce_cost* los* n_*

forval year=2005(1)2017{
replace fce_cost_em`year' = fce_cost`year' - fce_cost_el`year'
}

save "$savedata/pastip_0417.dta", replace
